using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using EntLibEC.Biz;
using EntLibEC.Biz.Basic;
using EntLibEC.Biz.Stock;
using EntLibEC.Cmn;
using EntLibEC.Info.Basic;
using EntLibEC.Info.Stock;

namespace EntLibEC.View.Basic
{
	/// <summary>
	/// Summary description for ProductPM.
	/// </summary>
	public partial class ProductPM : PageBase
	{
		protected EntLibEC.View.Basic.ProductLeftBar ucLeftBar;
		protected int SysNo = AppConst.IntNull;
		protected EntLibEC.View.Cmn.PM ucPPM;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(Request.QueryString["SysNo"]!=null&&Util.IsNaturalNumber(Request.QueryString["SysNo"]))
				SysNo = int.Parse(Request.QueryString["SysNo"]);
			if(!IsPostBack)
				InitPage();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private void InitPage()
		{
			ucLeftBar.ProductSysNo = SysNo;
			ucLeftBar.InitTable();
			ProductBasicInfo pb = ProductManager.GetInstance().LoadBasic(SysNo);
			txtNote.Text = pb.Note;
			txtProductID.Text = pb.ProductID;
			txtProductName.Text = pb.ProductName;
			txtWarranty.Text = pb.Warranty;			
			lblStatus.Text = ProductManager.GetInstance().GetProductStatusDesc(pb.Status);
			ucPPM.BindPM(true);
			ucPPM.PMSysNo = pb.PPMUserSysNo;
			ProductStatusInfo ps = ProductManager.GetInstance().LoadStatus(SysNo);
			if(ps.AllowStatus==(int)AppEnum.ProductStatusInfo.Show)
				chkAllowShow.Checked = true;
			else
				chkAllowShow.Checked = false;
			if(ps.WarrantyStatus==(int)AppEnum.ProductStatusInfo.Show)
				chkWarranty.Checked = true;
			else
				chkWarranty.Checked = false;
		}

		protected void btnUpdate_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.ProductPM, false, lblmsg) )
				return;

			if(txtWarranty.Text.Trim()==string.Empty)
			{
				Assert(lblmsg,"Warranty can't be null",1);
				return;
			}
			ProductBasicInfo pb = ProductManager.GetInstance().LoadBasic(SysNo);
			pb.Warranty = txtWarranty.Text.Trim();
			pb.Note = txtNote.Text.Trim();
			pb.PPMUserSysNo = ucPPM.PMSysNo;
			pb.PMUserSysNo = ucPPM.PMSysNo;
			ProductStatusInfo ps = ProductManager.GetInstance().LoadStatus(SysNo);
			if(chkWarranty.Checked)
				ps.WarrantyStatus = (int)AppEnum.ProductStatusInfo.Show;
			else
				ps.WarrantyStatus = (int)AppEnum.ProductStatusInfo.Valid;
			ps.WarrantyTime = DateTime.Now;
			ps.WarrantyUserSysNo = this.sInfo.User.SysNo;
			if(chkAllowShow.Checked)
				ps.AllowStatus = (int)AppEnum.ProductStatusInfo.Show;
			else
				ps.AllowStatus = (int)AppEnum.ProductStatusInfo.Valid;
			ps.AllowTime = DateTime.Now;
			ps.AllowUserSysNo = this.sInfo.User.SysNo;
			try
			{
				ProductManager.GetInstance().EditProduct(pb,ps);
				LogManager.GetInstance().Write(new LogInfo(pb.SysNo,(int)AppEnum.LogType.Basic_Product_PM_Update,base.sInfo));
				Assert(lblmsg,"Update OK",1);
			}
			catch(Exception ex)
			{
				Assert(lblmsg,ex.Message,-1);
			}
		}
	}
}
